The Node.js® Website
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 72 lines 2.9 kB view raw
1import { deflateSync } from 'node:zlib'; 2 3import matter from 'gray-matter'; 4 5import { VERCEL_REVALIDATE } from '@/next.constants.mjs'; 6import { dynamicRouter } from '@/next.dynamic.mjs'; 7import { defaultLocale } from '@/next.locales.mjs'; 8import { parseRichTextIntoPlainText } from '@/util/stringUtils'; 9 10// This is the Route Handler for the `GET` method which handles the request 11// for a digest and metadata of all existing pages on Node.js Website 12// @see https://nextjs.org/docs/app/building-your-application/routing/router-handlers 13export const GET = async () => { 14 const allAvailbleRoutes = await dynamicRouter.getRoutesByLanguage( 15 defaultLocale.code 16 ); 17 18 const availablePagesMetadata = allAvailbleRoutes 19 .filter(route => !route.startsWith('blog')) 20 .map(async pathname => { 21 const { source, filename } = await dynamicRouter.getMarkdownFile( 22 defaultLocale.code, 23 pathname 24 ); 25 26 // Gets the title and the Description from the Page Metadata 27 const { title, description } = await dynamicRouter.getPageMetadata( 28 defaultLocale.code, 29 pathname 30 ); 31 32 // Parser the Markdown source with `gray-matter` and then only 33 // grabs the markdown content and cleanses it by removing HTML/JSX tags 34 // removing empty/blank lines or lines just with spaces and trims each line 35 // from leading and trailing paddings/spaces 36 const cleanedContent = parseRichTextIntoPlainText(matter(source).content); 37 38 // Deflates a String into a base64 string-encoded (zlib compressed) 39 const deflatedSource = deflateSync(cleanedContent).toString('base64'); 40 41 // Returns metadata of each page available on the Website 42 return { 43 filename, 44 pathname, 45 title, 46 description, 47 content: deflatedSource, 48 }; 49 }); 50 51 return Response.json(await Promise.all(availablePagesMetadata)); 52}; 53 54// This function generates the static paths that come from the dynamic segments 55// `[locale]/next-data/page-data/` and returns an array of all available static paths 56// This is used for ISR static validation and generation 57export const generateStaticParams = async () => [ 58 { locale: defaultLocale.code }, 59]; 60 61// Enforces that only the paths from `generateStaticParams` are allowed, giving 404 on the contrary 62// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams 63export const dynamicParams = false; 64 65// Enforces that this route is used as static rendering 66// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic 67export const dynamic = 'error'; 68 69// Ensures that this endpoint is invalidated and re-executed every X minutes 70// so that when new deployments happen, the data is refreshed 71// @see https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#revalidate 72export const revalidate = VERCEL_REVALIDATE;